package com.zengyan.gd.controller;

import com.zengyan.gd.core.Result;
import com.zengyan.gd.core.ResultGenerator;
import com.zengyan.gd.core.ServiceException;
import com.zengyan.gd.form.UserLoginForm;
import com.zengyan.gd.model.SignUp;
import com.zengyan.gd.model.User;
import com.zengyan.gd.service.UserService;
import com.zengyan.gd.utils.ImageUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import javax.validation.Valid;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    UserService mUserService;


    /**
     * @api {post}  /user/login  用户登陆（学号密码登陆即可）
     * @apiVersion 0.0.1
     * @apiGroup user
     */
    @PostMapping("/login")
    public Result login(@RequestBody @Valid UserLoginForm loginForm, BindingResult bindingResult) throws Exception {
//        User user = mUserService.login(loginForm);
        return ResultGenerator.genSuccessResult(mUserService.getUser("201430098034"), "用户登陆成功");
    }

    /**
     * @api {get}  /user/{stunum}/orgs  获取一个用户参加的所以组织
     * @apiVersion 0.0.1
     * @apiGroup user
     */
    @GetMapping("/{stunum}/orgs")
    public Result getOrgs(@PathVariable("stunum") String stuNum) throws Exception {
        return ResultGenerator.genSuccessResult(mUserService.getAllOrg_I_Join(stuNum));
    }

    /**
     * @api {get}  /user/{stunum}  获取一个用户的详细信息
     * @apiVersion 0.0.1
     * @apiGroup user
     */
    @GetMapping("/{stunum}")
    public Result getUser(@PathVariable("stunum") String stunum) throws Exception {
        return ResultGenerator.genSuccessResult(mUserService.getUser(stunum));
    }

    /**
     * @api {post}  /user/update/image  用户更新图片
     * @apiVersion 0.0.1
     * @apiGroup user
     * @apiParam stunum    String     学号
     * @apiParam file  png,jpg 文件
     * @apiParam type  String  userAvatar 或者 userBg
     */
    @PostMapping("/update/image")
    public Result uploadAvatar(@RequestParam("file") MultipartFile file,
                               @RequestParam("stunum") String stuNum,
                               @RequestParam("type") String type) throws Exception {

        User user = mUserService.updateImage(stuNum, file, ImageUtils.getImageType(type));
        return ResultGenerator.genSuccessResult(user);
    }

    @GetMapping("/{stunum}/search")
    public Result searchPeople(@PathVariable("stunum") String stunum, @RequestParam("message") String message) throws Exception {
        List<User> list = mUserService.searchPeople(stunum, message);
        return ResultGenerator.genSuccessResult(list);
    }

    /**
     * @api {post}  /user/signup  报名
     * @apiVersion 0.0.1
     * @apiGroup user
     */
    @PostMapping("/signup")
    public Result signUp(@RequestBody @Valid SignUp signUp, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) throw new ServiceException(bindingResult);
        return ResultGenerator.genSuccessResult(mUserService.signUpInEvent(signUp));
    }

    /**
     * @api {get}  /user/event/all  得到活动的feed
     * @apiVersion 0.0.1
     * @apiGroup user
     */
    @GetMapping("/event/all/{stunum}")
    public Result getAllEvent(@PathVariable("stunum") String stunum) throws Exception {
        return ResultGenerator.genSuccessResult(mUserService.getAllEvent(stunum));
    }

    @GetMapping("/event/{stunum}")
    public Result getMyEvents(@PathVariable("stunum") String stunum) throws Exception {
        return ResultGenerator.genSuccessResult(mUserService.getEventIjoin(stunum));
    }




}